package com.shushan.home.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shushan.lockscreen.bean.AppInfo;
import com.shushan.shushanbao.R;

import java.util.List;

/**
 * @ClassName: WhiteAppsAdapter
 * @Desciption: 白名单adapter
 * @author: yichaohua
 * @date: 2018-07-20
 */
public class WhiteAppsAdapter extends BaseAdapter{

    private Context context;
    private List<AppInfo> appInfoList;

    public WhiteAppsAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if(appInfoList == null){
            return 0;
        }else {
            return appInfoList.size();
        }

    }

    @Override
    public Object getItem(int position) {
        if(appInfoList == null){
            return null;
        }else {
            return appInfoList.get(position);
        }

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            //填充布局
            convertView =  LayoutInflater.from(context).inflate(R.layout.child_white_app_list_item, parent,false);
            holder = new ViewHolder();
            //通过ID查询控件
            holder.nameTextView = convertView.findViewById(R.id.child_white_list_item_tv);
            holder.logoImg = convertView.findViewById(R.id.child_white_list_item_img);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        AppInfo appInfo = appInfoList.get(position);

        Drawable logo = appInfo.getIcon();
        holder.logoImg.setImageDrawable(logo);
        holder.nameTextView.setText(appInfo.getLabel());

        return convertView;
    }

    public static class ViewHolder{
        TextView nameTextView;
        ImageView logoImg;
    }

    public List<AppInfo> getAppInfoList() {
        return appInfoList;
    }

    public void setAppInfoList(List<AppInfo> appInfoList) {
        this.appInfoList = appInfoList;
    }
}
